package com.teradata.dashboard.ui.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.teradata.dashboard.R;
import com.teradata.dashboard.utils.HttpUtil;


import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

public class MyComments extends Activity {
    JSONObject jsonObjComments;
    JSONArray commentsData;
    JSONObject comment;
    
	private ListView lv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mycomments);
		

		lv = (ListView) findViewById(R.id.commentList);     
		
		try {
			jsonObjComments = queryComments();
			commentsData = jsonObjComments.getJSONArray("data");
			/*定义一个动态数组*/     
			 ArrayList<HashMap<String, Object>> listItem = 
					 new ArrayList<HashMap<String,     Object>>();
			/*在数组中存放数据*/
			 for(int i=0;i<commentsData.length();i++)  
			        {  
			            HashMap<String, Object> map = new HashMap<String, Object>();  
			            comment = commentsData.getJSONObject(i);
			            
			            map.put("commentImg", R.drawable.ic_launcher);
			            map.put("userName", comment.get("publishUserName"));
			            map.put("position", comment.get("publishUserBranchName"));  
			            map.put("commentTime", comment.get("publishDatetime"));  
			            map.put("commentContent", comment.get("bulletinDetail"));  
			            
			            listItem.add(map);  

			        }

			 
		  SimpleAdapter mSimpleAdapter =new SimpleAdapter(this,listItem,               
				 R.layout.commentitems,
				 new String[] {"commentImg"
			 ,"userName", "position", "commentTime", "commentContent"},   
			 new int[] {R.id.commentImg,R.id.userName,R.id.position, R.id.commentTime, R.id.commentContent}  );
		 
		  lv.setAdapter(mSimpleAdapter);//为ListView绑定适配器 lv.setOnItemClickListener(new 
		  
		  lv.setOnItemClickListener(new 
				  OnItemClickListener() {

				              @Override
				              public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				                      long arg3) {
				                  setTitle("你点击了第"+arg2+"行");//设置标题栏显示点击的行                
				              }
				          });
		
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}	
	}
	
	private JSONObject queryComments()
			throws Exception
		{
			//使用Map封装请求参数
			Map<String, String> map = new HashMap<String, String>();
			
			// 定义发送请求的URL
			String url = HttpUtil.BASE_URL + "dashboard/listKpiBulletin.action";
			// 发送请求
			return new JSONObject(HttpUtil.postRequest(url, map));
		}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.kpi_menu, menu);
		return true;
	}

}
